package com.example.javademo;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import java.util.ArrayList;
import java.util.List;

public class Viewpage2 extends AppCompatActivity {

    List<Integer> pice = new ArrayList<>();
    ViewPager2 pager2;
    LinearLayout indicator_container;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_viewpage2);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        pice.add(R.drawable.banner1);
        pice.add(R.drawable.banner2);
        pice.add(R.drawable.banner3);
        pice.add(R.drawable.banner4);

//        创建适配器
        RecyclerView.Adapter adapter = new RecyclerView.Adapter() {

            /*创建*/
            @NonNull
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View v = LayoutInflater.from(Viewpage2.this).inflate(R.layout.viewpage2_item, parent, false);
                return new ViewHolder(v);
            }

            /*绑定 ViewHolder里面的控件显示内容 */
            @Override
            public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
                ViewHolder h = (ViewHolder) holder;
                h.itemView.setBackgroundResource(pice.get(position));
                h.itemView.setOnClickListener(v -> {
                    pager2.setCurrentItem(position);
                });
            }

            /*数量*/
            @Override
            public int getItemCount() {
                return pice.size();
            }
        };

        pager2 = findViewById(R.id.viewpager);
        pager2.setAdapter(adapter);
        indicator_container = findViewById(R.id.indicator_container);

        // 设置初始选中状态
        if (indicator_container.getChildCount() > 0) {
            ((ImageView) indicator_container.getChildAt(0)).setBackgroundResource(R.drawable.selected_dot);
        }

        pager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                Log.d("ViewPager2", "Page selected: " + position);

                // 边界检查
                if (indicator_container.getChildCount() <= position || position < 0) {
                    Log.e("ViewPager2", "Invalid position: " + position);
                    return;
                }

                // 设置所有指示器为未选中状态
                for (int i = 0; i < indicator_container.getChildCount(); i++) {
                    View child = indicator_container.getChildAt(i);
                    if (child instanceof ImageView) {
                        Log.d("ViewPager2", "Setting unselected dot for index: " + i);
                        ((ImageView) child).setImageResource(R.drawable.unselected_dot);
                    }
                }

                // 设置当前选中项为选中状态
                View selectedChild = indicator_container.getChildAt(position);
                if (selectedChild instanceof ImageView) {
                    Log.d("ViewPager2", "Setting selected dot for index: " + position);
                    ((ImageView) selectedChild).setImageResource(R.drawable.selected_dot);
                }
            }
        });
    }

    public  class ViewHolder extends RecyclerView.ViewHolder{
        public LinearLayout container;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            container = itemView.findViewById(R.id.containerViewpage2);
        }
    }
}